<template>
  <div class="register-container">
    <el-card class="register-card">
      <template #header>
        <div class="card-header">
          <h2>用户注册</h2>
        </div>
      </template>

      <el-form
        :model="formState"
        label-position="top"
        @submit.prevent="onFinish"
      >
        <el-form-item
          label="用户名"
          prop="userName"
          :rules="[{ required: true, message: '请输入用户名', trigger: 'blur' }]"
        >
          <el-input
            v-model="formState.userName"
            placeholder="请输入用户名"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item
          label="密码"
          prop="userPassword"
          :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]"
        >
          <el-input
            v-model="formState.userPassword"
            type="password"
            placeholder="请输入密码"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item
          label="确认密码"
          prop="confirmPassword"
          :rules="[
            { required: true, message: '请再次输入密码', trigger: 'blur' },
            { validator: validateConfirmPassword }
          ]"
        >
          <el-input
            v-model="formState.confirmPassword"
            type="password"
            placeholder="请确认密码"
            show-password
          />
        </el-form-item>

        <el-form-item
          label="邮箱"
          prop="email"
          :rules="[
            { required: true, message: '请输入邮箱', trigger: 'blur' },
            { type: 'email', message: '请输入有效的邮箱地址', trigger: 'blur' }
          ]"
        >
          <el-input
            v-model="formState.email"
            placeholder="请输入邮箱"
          >
            <template #prefix>
              <el-icon><Message /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item class="form-actions">
          <el-button
            type="primary"
            native-type="submit"
            class="register-button"
          >
            注册
          </el-button>
          <router-link to="/login">
            <el-button link>返回登录</el-button>
          </router-link>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Message } from '@element-plus/icons-vue'
import { register } from '../api/auth'

const router = useRouter()
const formState = reactive({
  userName: '',
  userPassword: '',
  confirmPassword: '',
  email: '',
})

const validateConfirmPassword = (rule, value, callback) => {
  if (value !== formState.userPassword) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

const onFinish = async () => {
  try {
    const response = await register(formState)
    if (response.code === "200") {
      ElMessage.success('注册成功')
      router.push('/login')
    }
  } catch (error) {
    ElMessage.error(error.message || '注册失败')
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5;
}

.register-card {
  width: 450px;
  :deep(.el-card__header) {
    background-color: #f5f7fa;
    border-bottom: 1px solid #e4e7ed;
  }
}

.form-actions {
  margin-top: 24px;
}

.register-button {
  width: 100%;
  margin-bottom: 12px;
}
</style>